package com.itheima.demo02;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class SendMassageDemo02 {
    public static void main(String[] args) throws IOException {
        //利用 UDP 创建聊天室

        //创建 UPD DatagramSocket 对象
        DatagramSocket ds = new DatagramSocket();
        //创建键盘录入数据
        Scanner scanner = new Scanner(System.in);

        while (true){
            System.out.println("请输入聊天内容:");
            String str = scanner.nextLine();
            if (str.equals("886")){
                break;
            }
            //打包数据
            byte[] bytes = str.getBytes();

            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            int port = 10087;

            DatagramPacket dp = new DatagramPacket(bytes,0,bytes.length,inetAddress,port);

            //发送数据
            ds.send(dp);
        }

        //释放资源
        ds.close();

    }
}
